package com.rjyb.student_management_platform.controller;

import com.rjyb.student_management_platform.pojo.Class;
import com.rjyb.student_management_platform.pojo.Teacher;
import com.rjyb.student_management_platform.pojo.VerifyCode;
import com.rjyb.student_management_platform.service.IVerifyCodeGen;
import com.rjyb.student_management_platform.service.Impl.SimpleCharVerifyCodeGenImpl;
import com.rjyb.student_management_platform.service.LoginService;
import com.rjyb.student_management_platform.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/login")
public class LoginController {
    @Autowired
    LoginService loginService;
    @Autowired
    TeacherService ts;

    //去登录界面
    @RequestMapping("/tologin")
    public String tologin() {
        return "teacher/lyear_pages_login";
    }

    //去老师主页
    @RequestMapping("/toteacherindex")
    public String toteacherindex(HttpSession session,Model model)  {
      //  List<Class> classList = ts.selectClass(session,model);
      //  model.addAttribute("classList",classList);
        //model.addAttribute("stu",null);
        Teacher teacher =(Teacher)session.getAttribute("teacher");
        long tid = teacher.getTid();
        long type = teacher.getType();
        if (type==2){
            //查看班级作业完成率
            List<Map> list = ts.selectClassGuideCompletionRate(tid);
            model.addAttribute("list2",list);
            return "teacher/index";
        }else {
            //查看班级作业完成率
            List<Map> list = ts.selectCompletionRate(tid);
            model.addAttribute("list2", list);
            return "teacher/index";
        }
    }

    //去学生主页
    @RequestMapping("/tostudentindex")
    public String tostudentindex(Model model,HttpSession session) {

        loginService.selectindex(model,session);
        return "student/index";
    }


    //登录验证
    @RequestMapping("/login")
    public String login(String code, Model model, String name, String password, HttpSession session, HttpServletRequest request) {

        return loginService.login(code, model, name, password, session, request);
    }




//老师退出登录
    @RequestMapping("/toteacherquit")
    public String toteacherquit(HttpSession session) {
        session.removeAttribute("teacher");
        System.out.println(session.getAttribute("teacher"));
        return "redirect:tologin";
    }

    //学生退出登录
    @RequestMapping("/tostudentquit")
    public String tostudentquit(HttpSession session) {
        session.removeAttribute("student");
        System.out.println(session.getAttribute("student"));
        return "redirect:tologin";
    }

//验证码
    @RequestMapping("/VerifyCode")
    public void verifycode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        IVerifyCodeGen iVerifyCodeGen = new SimpleCharVerifyCodeGenImpl();
        try {
//设置长宽
            VerifyCode verifyCode = iVerifyCodeGen.generate(80, 28);
            String code = verifyCode.getCode();

//将VerifyCode绑定session
            request.getSession().setAttribute("VerifyCode", code);
            System.out.println(code);
//设置响应头
            response.setHeader("Pragma", "no-cache");
//设置响应头
            response.setHeader("Cache-Control", "no-cache");
//在代理服务器端防止缓冲
            response.setDateHeader("Expires", 0);
//设置响应内容类型
            response.setContentType("image/jpeg");
            response.getOutputStream().write(verifyCode.getImgBytes());
            response.getOutputStream().flush();
        } catch (IOException ignored) {

        }
    }

}
